package com.duoketech.controller.weixin;

import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.interceptor.WxCartInterceptor;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.member.Member;
import com.duoketech.model.memberinfo.MemberInfo;
import com.duoketech.model.order.Order;
import com.duoketech.model.receiver.Receiver;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.web.member.MemberService;
import com.duoketech.service.weixin.ReceiverService;
import com.duoketech.service.weixin.activity.WeiXinActivityService;
import com.duoketech.service.weixin.member.WxMemberService;
import com.duoketech.service.weixin.memberinfo.WxMemberInfoService;
import com.duoketech.service.weixin.order.WxOrderService;
import com.duoketech.service.weixin.room.WeiXinRoomService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

public class WxMineController extends BaseController {
    @Before(WxCartInterceptor.class)
    public void index(){
        Member member = WxMemberService.ME.queryMemberById(getMemberId());
        setAttr("member",member);
        render("index.html");
    }

    public void mineActivity(){
        render("mine-activity.html");
    }

    public void mineRoom(){
        render("mine-room.html");
    }

    public void ajaxMineActivity(){
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Page<ActivityOrder> activtyOrder = WeiXinActivityService.ME.queryActivtyOfMine(pageNum,pageSize,getMemberId());
        renderObject(activtyOrder);
    }
    public void ajaxMineRoom(){
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Page<RoomOrder> roomOfMine = WeiXinRoomService.ME.queryUnitRoomOfMine(pageNum,pageSize,getMemberId());
        renderObject(roomOfMine);
    }

    public void mineOrder(){
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        int memberId = getMemberId();
        int status = getParaToInt("status",1);
        Page<Order> orderPage = WxOrderService.ME.queryOrdersOfMineBy(pageNum,pageSize,memberId,status);
        setAttr("orderPage",orderPage);
        render("mine-order.html");
    }

    public void mineInfo(){
        MemberInfo temp = WxMemberInfoService.ME.getMemberInfo(getMemberId());
        setAttr("memberInfo",temp);
        render("mine-info.html");
    }

    public void queryOrderOfMine(){
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        int memberId = getMemberId();
        int status = getParaToInt("status",1);
        Page<Order> orderPage = WxOrderService.ME.queryOrdersOfMineBy(pageNum,pageSize,memberId,status);
        renderObject(orderPage);
    }

    public void doUpdateMemberInfo(){
        MemberInfo memberInfo = getModel(MemberInfo.class,"");
        try {
            WxMemberInfoService.ME.doUpdateMemberInfo(memberInfo,getMemberId());
            renderSuccess("保存成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void resetPass(){

        render("reset-pass.html");
    }

    public void doResetPass(){
        String oldPass = getPara("oldPass","");
        String newPass = getPara("newPass","");
        String confirmPass = getPara("confirmPass","");
        try {
            WxMemberInfoService.ME.doResetPass(getMemberId(),oldPass,newPass,confirmPass);
            renderSuccess("修改密码成功,请在下次登录的时候使用新密码登录");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void bindMobile(){
        setAttr("member",Member.dao.findById(getMemberId()));
        render("bind-mobile.html");
    }

    public void doBindMobile(){
        String mobile = getPara("mobile","");
        try {
            Member member= WxMemberInfoService.ME.doBindMobile(mobile,getMemberId());
            setMember(member);
            renderSuccess("绑定成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void aboutus(){
        render("about-us.html");
    }
}
